<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="服务名称" prop="servicesName">
                <el-input v-model="queryParams.servicesName" placeholder="请输入服务名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" 
                v-hasPermi="['system:services:list']">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:services:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:services:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:services:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:services:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="服务名称" align="center" prop="servicesName" show-overflow-tooltip />
            <el-table-column label="服务描述" align="center" prop="servicesDesc" show-overflow-tooltip />
            <el-table-column label="服务价格（负数为优惠）" align="center" prop="price" />
            <el-table-column label="原始价格" align="center" prop="originalPrice" />
            <el-table-column label="是否为活动商品" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" size="small" v-if="scope.row.haveActivity == true">是</el-tag>
                    <el-tag type="danger" size="small" v-if="scope.row.haveActivity == false">否</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="服务状态" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" size="small" v-if="scope.row.status == true">启用</el-tag>
                    <el-tag type="info" size="small" v-if="scope.row.status == false">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" 
                    v-hasPermi="['system:services:query']">详情</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" 
                    v-hasPermi="['system:services:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" 
                    v-hasPermi="['system:services:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改套餐配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="服务名称" prop="servicesName">
                            <el-input v-model="form.servicesName" placeholder="请输入服务名称" />
                        </el-form-item>
                        <el-form-item label="服务价格" prop="price">
                            <el-input v-model="form.price" placeholder="请输入套餐金额（负数为优惠）" />
                        </el-form-item>
                        <el-form-item label="服务状态" prop="status">
                            <el-switch v-model="form.status"></el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="服务描述" prop="servicesDesc">
                            <el-input v-model="form.servicesDesc" placeholder="请输入服务描述" />
                        </el-form-item>
                        <el-form-item label="原始价格" prop="originalPrice">
                            <el-input v-model="form.originalPrice" placeholder="请输入原始价格" />
                        </el-form-item>
                        <el-form-item label="是否为活动商品" prop="haveActivity">
                            <el-switch v-model="form.haveActivity"></el-switch>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="title != '服务信息详情'">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listService, addService, updateService, getService, delService } from "@/api/business/service";
export default {
    // 组件名称
    name: 'service',
    // 组件参数 接收来自父组件的数据
    props: {},
    // 局部注册的组件
    components: {},
    // 组件状态值
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 套餐配置表格数据
            serviceList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                servicesName: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                servicesName: [{ required: true, message: '请输入服务名称', trigger: 'blur' }],
                servicesDesc: [{ required: true, message: '请输入服务描述', trigger: 'blur' }],
                price: [{ required: true, message: '请输入服务价格', trigger: 'blur' }],
                originalPrice: [{ required: true, message: '请输入服务原始价格', trigger: 'blur' }],
            }
        }
    },
    // 计算属性
    computed: {},
    // 侦听器
    watch: {},
    // 组件方法
    created() {
        this.getList();
    },
    mounted() { },
    methods: {
        /** 查询套餐配置列表 */
        getList() {
            this.loading = false;
            listService(this.queryParams).then(response => {
                console.log('response--------: ', response)
                this.serviceList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
       
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                servicesName: '',
                servicesDesc: '',
                price: '',
                originalPrice: '',
                status: true,
                haveActivity: true,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.servicesId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /**详情按钮操作 */
        handleDetail(row) {
            this.reset();
            const id = row.servicesId || this.ids
            getService(id).then(response => {
                console.log('response-------: ', response)
               
                this.form = response.data;
                this.open = true;
                this.title = "服务信息详情";
            });
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加套餐配置";
           
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.servicesId || this.ids
            getService(id).then(response => {
                console.log('response-------: ', response)
              
                this.form = response.data;
                this.open = true;
                this.title = "修改服务信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            console.log('this.form---------: ', this.form)
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.servicesId != null) {
                        updateService(this.form.servicesId, this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addService(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.servicesId || this.ids;
            this.$modal.confirm('是否确认删除套餐配置编号为"' + ids + '"的数据项？').then(function () {
                return delService(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/system/services/export', {
                ...this.queryParams
            }, `services_${new Date().getTime()}.xlsx`)
        }
    },
}
</script> 

<style scoped></style>
